/*
 *所有关于Erp_mara类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Erp_maraService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MyErp.IRepositories;
using System.Collections.Generic;
using System;
using VOL.Core.ManageUser;

namespace MyErp.Services
{
    public partial class Erp_maraService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IErp_maraRepository _repository;//访问数据库
        WebResponseContent webResponse = new WebResponseContent();
        [ActivatorUtilitiesConstructor]
        public Erp_maraService(
            IErp_maraRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        public override WebResponseContent Update(SaveModel saveModel)
        {

            //编辑方法保存数据库前处理,只能在修改之前取数，因为需要取原始值
            UpdateOnExecuting = (Erp_mara currList, object addList, object updateList, List<object> delKeys) =>
            {
                //工艺数据字段更改记录，原值！=现值才记录
                string t = (_repository.Find(t => t.mara_id == currList.mara_id).Select(s => s.gongyi).FirstOrDefault()??"").ToString();//注意原值为空的情况??""
                string gongyi_old = t;
                string gongyi_new = currList.gongyi;
                if (gongyi_old != gongyi_new)
                {
                    string xgren = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
                    Console.WriteLine(xgren + " 修改了物料名称为：" + currList.maktx + "的工艺字段，原值为：" + gongyi_old + " 现值为：" + gongyi_new + " 修改时间为：" + DateTime.Now);
                }
                return webResponse.OK();
            };
            return base.Update(saveModel);
        }

        public override WebResponseContent Del(object[] keys, bool delList = true)
        {

            string delstr = _repository.Find(t => t.mara_id == keys[0].ToString().ToGuid()).Select(s => s.maktx).FirstOrDefault();
            string xgren = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
            Console.WriteLine(xgren + " 删除物料名称为：" + delstr + " 删除时间为：" + DateTime.Now);
            return base.Del(keys, delList);
        }
        public override WebResponseContent Add(SaveModel saveDataModel)
        {

            AddOnExecuted = (Erp_mara currList, object list) =>
            {
                string xgren = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
                Console.WriteLine(xgren + "新增物料名称为：" + currList.maktx + "，新增时间为：" + DateTime.Now);

                return webResponse.OK("保存成功！");//必须要加，vol的bug
            };
            return base.Add(saveDataModel);
        }
    }
}
